#include <iostream>
#include <mysql/mysql.h>

using namespace std;

int main(){

	MYSQL *pdata;
	MYSQL pp;
	mysql_get_server_info( &pp );
	MYSQL_RES *result;
	MYSQL_ROW row;

	//init
	pdata = mysql_init( NULL );

	if ( NULL == pdata ){
		cout<<"Init failed." <<endl;
		return -1;
	}

	//connect
	pdata = mysql_real_connect( pdata,"localhost", "root", "zfdhr$7758521", "work", MYSQL_PORT, NULL, 0 );
	if ( NULL == pdata ){
		cout << "Connect failed." <<endl;
		return -1;
	}

	//query
	int iq = mysql_query( pdata, "select `word`,`pos` from keyword" );
	if ( iq != 0 ){
		cout << "Query failed." <<endl;
		return -1;
	}

	//get result
	result = mysql_store_result( pdata );
	if ( NULL == result ){
		cout << " Get result failed." <<endl;
		return -1;
	}


	//show field content
	int iNum = mysql_num_fields(result);
	while( row = mysql_fetch_row( result ) ){

		for ( int ix=0; ix < iNum; ++ix ){
			if( row[ix] ){
				cout << row[ ix ] << " ";
			}
		}
		cout <<endl;
	}

	iq = mysql_query( pdata , "insert into keyword( `word`,`pos` ) values( \"hello\" , \"ty\");");
	if( 0!=iq ){
		cout << "Insert failed." <<endl;
	}

	mysql_free_result( result );
	mysql_close( pdata );

	return 0;
}
